require("SOD/tools/OpenStudio/Collection")
require("SOD/FileManager")

module SOD
  
  class ProjectManager
  
    attr_reader :projectList, :projectFileList
    attr_reader :activeProjectName, :activeFileName
    attr_reader :activeProjectPath, :activeFilePath
    attr_reader :validate, :fileManager
    attr_reader :open, :save, :saveAs
    attr_reader :openFile, :saveFile, :resetFiles, :setActivePath
  
    def initialize
      @fileManager = FileManager.new
      @projectFileList = Collection.new
      @activeProjectName = nil
      @activeProjectPath = nil
      @activeFileName = nil
      @activeFilePath = nil
    end
    
    def resetFiles
      @activeFileName = "No Variant loaded"
      @activeFilePath = if @activeProjectName.nil?
        nil
      else
        @activeProjectPath + "/" + @activeProjectName
      end
      updateProjectFiles
    end
    
    def updateProjectFiles
      Dir.chdir @activeProjectPath
      @activeProjectDir = Dir.new(@activeProjectName)
      @projectFileList.clear
      @activeProjectDir.each do |variant|
        variant.chomp!(".xml")
        @projectFileList.add(variant)
      end
      @projectFileList.shift
      @projectFileList.shift
      puts @activeProjectPath, @activeProjectName, @projectFileList if $debug
    end
    
    def openFile
      # included in dilogs
      @prompts = [@activeProjectName]
      @defaults = [""]
      @list = [@projectFileList.join("|")]
      
      @activeFileName = UI.inputbox @prompts, @defaults, @list, "Load File"
      return if not @activeFileName # Verhalten beim Abbruch
      Plugin.model.discardModel
      puts @activeFileName = @activeFileName.to_s
      puts @activeFilePath = @activeProjectPath + "/" + @activeProjectName
      
      @fileManager.readFile(self)
    end
    
    def saveFile
      Dir.chdir @activeProjectPath
      @activeProjectDir = Dir.new(@activeProjectName)
      if @activeProjectDir.nil?
        Dir.mkdir @activeProjectName
        newFile
      else
        if checkFiles
          result = UI.messagebox("File is existing already \n Do you like to overwrite it?", MB_YESNO)
          if result == 6
            @fileManager.writeDataToFile(self)
          else
            newFile
          end
        else
          newFile
        end
        puts "saved" if $debug
      end
      updateProjectFiles
      UI.messagebox( "File " + @activeFileName + " saved!" )
    end
    
    def newFile
      fileNumber = @projectFileList.length
      fileNumber += 1
      fileNumber = 1 if fileNumber.nil?
      print "generated filenumber = #{fileNumber}\n" if $debug
      
      @activeFileName = "Variant" + fileNumber.to_s
      file = File.new("./" + @activeProjectName + "/" + @activeFileName + ".xml", "w+")
      @fileManager.set_default_file(file)
      file.close
      
      @activeFilePath = @activeProjectPath + "/" + @activeProjectName
      puts @activeFilePath if $debug
      
      @fileManager.writeDataToFile(self)
      
    end
    
    def checkFiles
      @projectFileList.each do |variant|
        if variant == @activeFileName
          return true
        end
      end
      return false
    end
    
    def save
      if @activeProjectName.nil?
        UI.messagebox "Project has to be saved somewhere first!"
        saveAs
      else
        @path = Plugin.dir + "/data/" + @activeProjectName + ".skp"
        return if not @path
        Plugin.newModel
        Sketchup.active_model.save(@path)
        puts @activeProjectName if $debug
      end
    end
    
    def saveAs
      @path = Plugin.dir + "/data/"
      Dir.chdir @path
      @saveName = "Project" + @projectFileList.length.to_s + ".skp"
      @savePath = UI.savepanel("Save Project", @path, @saveName)
      puts @savePath if $debug
      return if not @savePath
      Plugin.newModel
      @savePath.gsub!(/\\/, '/')
      @savePath.chomp!(".skp")
      @savePath = @savePath + ".skp"
      Sketchup.active_model.save(@savePath)
      setActiveProject(@savePath)
      Dir.mkdir @activeProjectName
      resetFiles
    end
    
    def open
      @path = Plugin.dir + "/data/"
      @filePath = UI.openpanel("Load Project", @path, "*.skp")
      return if not @filePath
      @filePath.gsub!(/\\/, '/')
      puts "opened path: " + @filePath.to_s if $debug
      Plugin.path = @filePath
      Sketchup.open_file(@filePath)
    end
  
    def setActiveProject(path)
      puts path if $debug
      @activeProjectName = File.basename(path, ".skp")
      puts "Set Name: " + @activeProjectName.to_s if $debug
      @activeProjectPath = path.chomp("/" + @activeProjectName + ".skp")
      puts "Set path: " + @activeProjectPath.to_s if $debug
    end
    
    def validate
      return @activeProjectName.nil?
    end
    
    def validateFiles
      return @projectFileList.empty?
    end
    
  end
  
end
